package com.itheima.controller;

import com.itheima.domain.User;
import com.itheima.service.UserService;
import com.itheima.service.impl.UserServiceImpl;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/*
    控制层测试类
 */
public class UserController {
    //创建业务层对象
    private UserService service = new UserServiceImpl();

    //设置全局变量
    private SqlSession session = null;

    //查询所有用户信息
    @Test
    public void selectAll() throws Exception {
        List<User> list = service.selectAll();
        //处理结果
        for (User user : list) {
            System.out.println(user);
        }
    }

    //根据id查询
    @Test
    public void seleqctById() throws Exception{
        User user = service.selectById(2);
        //处理结果
        System.out.println(user);
    }

    //添加用户信息
    @Test
    public void insertUser() throws Exception {
        User user = new User();
        user.setUname("李武");
        user.setUpwd("123");
        user.setSex("男");
        service.insertUser(user);
    }

    //修改用户信息
    @Test
    public void updateUser() throws Exception {
        User user = service.selectById( 15);
        user.setUname("李月");
        user.setUpwd("333");
        user.setSex("女");
        service.updateUser(user);
    }

    //删除用户信息
    @Test
    public void deleteUser() throws Exception {
        service.deleteUser(14);
    }

    //登录操作
    @Test
    public void findByNameAndPwd() throws Exception {
        Map<String,String> map = new HashMap<String, String>();
        map.put("uname","李月");
        map.put("upwd","333");
        User user = service.findByNameAndPwd(map);
        System.out.println(user);
    }

    //统计男女人数
    @Test
    public void findSex() throws Exception {
        List<Map<String,Object>> list = service.findSex();
        Map<String,Long> map = new HashMap<String, Long>();
        for (Map<String, Object> mp : list) {
            map.put((String)mp.get("sex"),(Long)mp.get("total"));
        }
        System.out.println(map);
    }

    //if判断查询用户信息
    @Test
    public void findCondition() throws Exception {
        List<User> list1 = service.findCondition();
        System.out.println(list1);
    }
    @Test
    public void findCondition1() throws Exception {
        List list = new ArrayList();
        list.add(1);
        List list1 = service.findCondition1(list);
        System.out.println(list1);
    }

    //统计人数
    @Test
    public void total() throws Exception {
        //4、调用具体的statement对象，执行sql
        int u = session.selectOne("UserMapper.total");
        //处理结果
        System.out.println(u);
    }

    //模糊查询1
    @Test
    public void findlikeName1() throws Exception {
        //4、调用具体的statement对象，执行sql
        List<User> list = session.selectList("UserMapper.findlikeName","%李%");
        //处理结果
        for (User user : list) {
            System.out.println(user);
        }
    }

    //模糊查询2
    @Test
    public void findlikeName2() throws Exception {
        //4、调用具体的statement对象，执行sql
        List<User> list = session.selectList("UserMapper.findlikeName2","李");
        //处理结果
        for (User user : list) {
            System.out.println(user);
        }
    }
}
